// System includes
#include <QPalette>
#include <QDebug>

// Local includes
#include "yigscsynthbootmenu.h"
#include "graphics/yigcolorkit.h"

YigBootMenuThread::YigBootMenuThread() :
    loadMovie(":/images/gui/yig-loader.gif")
{
    running = false;
    QObject::connect(&timer, SIGNAL(timeout()), &loadMovie, SLOT(jumpToNextFrame()));
}

void YigBootMenuThread::start()
{
    running = true;
    loadMovie.start();
    timer.start(33);
    QThread::start();
}

void YigBootMenuThread::run()
{
    exec();
}

YigSCSynthBootMenu::YigSCSynthBootMenu(QWidget *parent) :
    QWidget(parent),
    menuText(this),
    movieLabel(this)
{
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint);
    setGeometry(0, 0, 300, 70);
    menuText.setGeometry(5, -15, 300, 60);
    menuText.setText("The SuperCollider scsynth server is booting...");
    //timer.setInterval(66);
    //QObject::connect(&timer, SIGNAL(timeout()), &loadMovie, SLOT(jumpToNextFrame()));
    //QObject::connect(&timer, SIGNAL(timeout()), this, SLOT(update()));
    /*
    QPalette yigPalette;
    yigPalette = menuText.palette();
    yigPalette.setColor(QPalette::WindowText, YigColorKit::focus1Highlight);
    setPalette(yigPalette);*/

    movieLabel.setGeometry(0, 0, 220, 19);
    movieLabel.move(geometry().width() / 2 - 110, 30);
    setFixedSize(size());
    move(QApplication::desktop()->screen()->rect().center() - rect().center());

    QPalette yigPalette = palette();
    QLinearGradient backgroundGradient(0, 0, 0, 70);
    backgroundGradient.setColorAt(0, YigColorKit::focus2Highlight);
    backgroundGradient.setColorAt(0.05, YigColorKit::focus1);
    backgroundGradient.setColorAt(0.95, YigColorKit::focus1);
    backgroundGradient.setColorAt(1, YigColorKit::focus2Highlight);
    backgroundGradient.setInterpolationMode(QGradient::ColorInterpolation);
    backgroundGradient.setInterpolationMode(QGradient::ColorInterpolation);
    QBrush backgroundBrush(backgroundGradient);

    yigPalette.setBrush(QPalette::Window, backgroundBrush);
    setPalette(yigPalette);

    if(movieThread.loadMovie.isValid())
    {
        movieLabel.setMovie(&movieThread.loadMovie);
        movieThread.start();
        //loadMovie.moveToThread(&movieThread);
        //timer.moveToThread(&movieThread);
        //loadMovie.start();
        //timer.start();
        //movieThread.start();
        //loadMovie.start();
    }

    else
    {
        qDebug() << "MOVIE NOT WORKING!";
    }
    //movieLabel.setMovie(&loadMovie);
    //loadMovie.start();
    show();
}

void YigSCSynthBootMenu::close()
{
    qDebug() << "SYNTH BOOT MENU CLOSE CALLED";
    //timer.stop();
    movieThread.running = false;
    movieThread.quit();
    QWidget::close();
}
